﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Turtle8
{
    public partial class CompanyMatzalPanel : UserControl
    {
        private CompanyCard cCard;
        DataTable dt;

        public CompanyMatzalPanel(CompanyCard cCard)
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;

            this.cCard = cCard;

            CompanyMatzalPanel_Load(null, null);
            dataGridView1.Columns["email"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            dataGridView1.Columns["city"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            CompanyManager.setTableColumnHeadersHebrew(dataGridView1.Columns);

            refresh();
        }

 
        private void CompanyMatzalPanel_Load(object sender, EventArgs e)
        {
            setLabels();
            showDataGridView();
        }
        private void showDataGridView()
        {
            string choice = CompanyManager.translateHebrewWord(comboBox.Text);
            SqlCommand selectCmd = new SqlCommand();

            switch (choice)
	        {
                case    "actives": selectCmd.CommandText = CompanyManager.getSqlStringFromFile(@"\SELECT\CompanyMatzalPanel.SELECT.active.sql")   ; break;
                case "notActives": selectCmd.CommandText = CompanyManager.getSqlStringFromFile(@"\SELECT\CompanyMatzalPanel.SELECT.NotActive.sql"); break;
                case        "all": selectCmd.CommandText = CompanyManager.getSqlStringFromFile(@"\SELECT\CompanyMatzalPanel.SELECT.all.sql"      ); break;
		        default: break;
	        }

            dt = CompanyManager.selectSqlQueryDataTable(selectCmd);
            dataGridView1.DataSource = dt;

            for (int i = 0; (i <= (dataGridView1.Rows.Count - 1)); i++)
                dataGridView1.Rows[i].HeaderCell.Value = string.Format((i + 1).ToString(), "0");
        }

        public void setLabels()
        {
            //display statistics of All/Active soldiers in company
            SqlCommand sqlCmdAll = new SqlCommand(@"SELECT COUNT(*) FROM ArmyInfo");
            SqlCommand sqlCmdActive = new SqlCommand(@"SELECT COUNT(*) FROM ArmyInfo WHERE isActive=1");
            textBoxAllSoldiers.Text = Convert.ToString(CompanyManager.selectCountSqlQuery(sqlCmdAll));
            textBoxActiveSoldiers.Text = Convert.ToString(CompanyManager.selectCountSqlQuery(sqlCmdActive));
        }

        public void refresh()
        {
            setLabels();
            showDataGridView();
        }

        private void buttonDisplay_Click(object sender, EventArgs e)
        {
            showDataGridView();
        }

        private void CompanyMatzalPanel_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
                refresh();
        }
       
    }
}

